package com.wds.controller;

import com.wds.common.annotation.SystemLog;
import com.wds.domain.JsonResult;
import com.wds.domain.entity.User;
import com.wds.exception.CustomException;
import com.wds.service.BlogLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WDs , wds8.24@outlook.com
 * @version 1.0
 * @since 2022-10-23 18:40
 */
@RestController
@Api(tags = "认证相关接口")
public class BlogLoginController {

    @Autowired
    private BlogLoginService loginService;


    @SystemLog(businessName = "请求登录认证")
    @PostMapping("/login")
    @ApiOperation("登录")
    public JsonResult<Object> login(@RequestBody User user) {
        // 用户名密码为空的情况
        if (!StringUtils.hasText(user.getUsername()) ||!StringUtils.hasText(user.getPassword())){
            throw new CustomException("请检查用户名和密码");
        }
        return loginService.login(user);
    }

    @PostMapping("/logout")
    public JsonResult<Object> logout(){
        return loginService.logout();
    }

}
